﻿using Microsoft.Extensions.DiagnosticAdapter;

namespace DiagnosticSourceDemo.Host
{
    public class MyObserver<T> : IObserver<T>
    {
        private Action<T> _next;
        public MyObserver(Action<T> next)
        {
            _next = next;
        }

        public void OnCompleted()
        {
        }

        public void OnError(Exception error)
        {
        }

        public void OnNext(T value) => _next(value);
    }

    public class MyDiagnosticListener
    {
        private readonly ServiceProvider _serviceProvider;

        public MyDiagnosticListener(IServiceCollection serviceCollection)
        {
            _serviceProvider = serviceCollection.BuildServiceProvider();
        }

        [DiagnosticName("System.Net.Http.Request")]
        public void HttpRequest(HttpRequestMessage request)
        {
            var httpContextAccessor = _serviceProvider.GetService<IHttpContextAccessor>();
            var httpContext = httpContextAccessor.HttpContext;
            Console.WriteLine($"request url: {httpContext.Request.Path},method: {httpContext.Request.Method}");
        }
    }
}
